// pages/releaseGuide/releaseGuide.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnValue:'发布',//默认发布
    successTip:'发布成功',
    messageValue:'',
    focus:true,
    price:'0',//默认免费
    selectTarIDArr:[],
    selectTarNameArr:[],
    receiveDic:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("接收到的参数是obj=" + options.receiveDic);//此处打印出来的仅仅是字符串 需要解析，解析如下
    this.setData({
      receiveDic: JSON.parse(options.receiveDic)
    })
    if (this.datareceiveDic.receiveDic.srcactionflag.length > 0){
      this.setData({
        btnValue:'投稿',
        successTip:'投稿成功'
      })
    }else{
      this.setData({
        btnValue: '发布',
        successTip:'发布成功'
      })
    }
  },

  //发布
  handlerSureTap:function(){
    var that = this;
    var tagValue = '';//
    if (that.data.selectTarNameArr.length > 0){
      tagValue = this.data.selectTarNameArr.toString();
    }
    wx.showLoading({})
    var dic = {
      scope_type:'1',//1公开 5 群动态
      title: that.data.receiveDic.title,
      description: that.data.receiveDic.description,
      cover: that.data.receiveDic.cover,
      music: that.data.receiveDic.music,
      music_name: that.data.receiveDic.musicName,
      message_title: that.data.messageValue,
      price: that.data.price,//价格
      category_ids: that.data.selectTarIDArr,//标签数组
      dest_action_flag: that.data.receiveDic.srcactionflag,//大赛id
      category_names: tagValue,
      show_type: that.data.receiveDic.show_type
    }
    console.log('发布dic' + dic)
    util.publicRequestWithToken(
      util.ReleaseKnowledge + that.data.receiveDic.knowledgeID + '/',
      'PUT',
      dic,
      //sucess
      function (data) {
          wx.showToast({
            title: that.data.successTip,
            icon: 'none'
          })
          //关闭当前页面跳转到知识页面
          wx.redirectTo({
            url: `../knowledge/knowledge?kgeId=${that.data.receiveDic.knowledgeID}&title=${that.data.receiveDic.title}`
          })
      },
      //fail
      function (error) {
        wx.showToast({
          title: '网络开小差了',
          icon: 'none'
        })
      },
      function () {
        // complete
        wx.hideLoading()
      }
    );
  },
  //设置 价格和标签
  setting:function(){
    const dic = {
      selectTarIDArr: this.data.selectTarIDArr,
      price: this.data.price,
    }
    wx.navigateTo({
      url: '../releaseSetting/releaseSetting?receiveDic=' + JSON.stringify(dic), //传的为对象
    })
  },
  //获取文本
  getAreaWord: function (e) {
    console.log(e.detail.value)
    this.setData({
      messageValue: e.detail.value
    })
  },
  //键盘完成事件
  losefocusKeybord: function () {
    this.setData({
      focus: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})